import requests,redis,pymysql,traceback

redis_line = redis.Redis(host='118.24.3.40',
                password='HK139bc&*',
                port='6379',
                db=4,
                decode_responses=True#默认redis返回的结果是字符串
)

MYSQL_INFO = {
    'host':'118.24.3.40',
    'user':'jxz',
    'password':'123456',
    'db':'jxz',
    'charset':'utf8',
    'autocommit':True
}#建立连接数据库信息常量
def execute_sql(sql):#执行sql函数
    conn = pymysql.connect(**MYSQL_INFO)#连接数据库
    cur = conn.cursor(pymysql.cursors.DictCursor)#建立游标,查询结果以字典格式输出

    try:
        cur.execute(sql)
    except:
        print('sql不正确')
        traceback.print_exc()#打印报错信息
    else:
        return cur.fetchall()  #返回sql查询出的结果
    finally:
        conn.close()
        cur.close()#关闭数据库连接

url = 'http://127.0.0.1:8888/login'
data = {'username':'gj','password':'123456'}
r = requests.post(url,data)
result = r.json()
sessionid = result['sessionid']

money = 200


if redis_line.get('sessionid')==None:
        print('请重新登录')
else:
    username = sessionid.split('_')[-1]
    query_sql = 'select * from gj_pay where username = "%s";' % (username)
    if execute_sql(query_sql):
        money-=90
        update_sql = 'update gj_pay set money="%s" where username="%s";'%(money,username)